package com.sjq.common.web;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.JsonObject;
import com.sjq.common.entity.LoginAccount;
import com.sjq.common.entity.User;
import com.sjq.common.service.RoleService;
import com.sjq.common.service.UserService;
import com.sjq.common.util.PageResults;

@Controller
public class UserController {

	@Autowired
    private UserService userService;
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private HttpSession session;  
	private static final String INITURL = "sys-manage/user-manage";
    
	//初始化
    @RequestMapping(INITURL)
    public ModelAndView init(Integer pageNo, Integer pageSize, User user){
    	if(user==null){
    		user=new User();
    	}
    	pageNo = pageNo == null ? 1 : pageNo;
        PageResults<?> pageResults=userService.findUserByPage(pageNo, PageResults.ALLPAGESIZE, user);
    	ModelAndView mav = new ModelAndView();
        mav.setViewName("/view/user/userList");
        mav.addObject("allUser", pageResults.getResults());
        mav.addObject("pageNo", pageResults.getPageNo());
        mav.addObject("pages", pageResults.getPageCount());
        return mav;
    }
    
    //跳转新增或修改
    @RequestMapping("/userModify")
    public @ResponseBody ModelAndView showUserModify(@RequestParam("userId") String userId) throws Exception{
    	if(userId.equals("-1")){
    		return new ModelAndView("view/user/userModify");
    	}else{
    		Map<String, Object> user=userService.findUserById(userId);
    		ModelAndView mav=new ModelAndView();
			mav.setViewName("view/user/userModify");
			mav.addObject("user",user);
			return mav;
    	}
    }
    
    //跳转详情
    @RequestMapping("/userDetails")
    public ModelAndView showUserDetails(@RequestParam("yhm") String yhm){
    	Map<String, Object> user=userService.findUserById(yhm);
		ModelAndView mav=new ModelAndView();
		mav.setViewName("view/user/userDetails");
		mav.addObject("user",user);
		return mav;
    }
    
    //新增用户
    @RequestMapping("/AddUser")
    public  ModelAndView addUserInfo(@RequestParam("xm") String xm,@RequestParam("gmsfhm") String gmsfhm,@RequestParam("jh") String jh,@RequestParam("dwmc") String dwmc,@RequestParam("dwbm") String dwbm,@RequestParam("lxfs") String lxfs) throws Exception{
    		User user = new User();
    		user.setXM(xm);
    		user.setYHM(gmsfhm);
    		user.setGMSFHM(gmsfhm);
    		user.setLXFS(lxfs);
    		user.setJH(jh);
    		user.setDWMC(dwmc);
    		user.setDWBM(dwbm);
    		userService.addUser(user);
    		ModelAndView mav=new ModelAndView("view/user/userModify");
			return mav;
    }
    
    //删除用户
    @RequestMapping("/delUser")
    public  ModelAndView delUserInfo(@RequestParam("yhm") String yhm ) throws Exception{
    		userService.removeUser(yhm);
    		ModelAndView mav=new ModelAndView("view/user/userList");
			return mav;
    }
    
    //修改用户
    @RequestMapping("/updateUser")
    public  ModelAndView updateUserInfo(@RequestParam("xm") String xm,@RequestParam("gmsfhm") String gmsfhm,@RequestParam("jh") String jh,@RequestParam("dwbm") String dwbm,@RequestParam("dwmc") String dwmc,@RequestParam("lxfs") String lxfs,@RequestParam("yhm") String yhm) throws Exception{
	    	User user = new User();
			user.setXM(xm.trim());
			user.setYHM(gmsfhm.trim());
			user.setGMSFHM(gmsfhm.trim());
			user.setLXFS(lxfs.trim());
			user.setJH(jh.trim());
			user.setDWBM(dwbm.trim());
			user.setDWMC(dwmc.trim());
			user.setYHM(yhm.trim());
			userService.modifyUser(user);
    		ModelAndView mav=new ModelAndView("view/user/userList");
			return mav;
    }
    
    //修改用户角色
    @RequestMapping("/userRole")
    public  ModelAndView userRoleInfo(@RequestParam("userId") String userId) throws Exception{
    		Map<String, Object> user = userService.findUserById(userId);
    		PageResults<?> rloeResults = roleService.selectAllRole();
    		ModelAndView mav=new ModelAndView();
			mav.setViewName("view/user/userRole");
			mav.addObject("allRole", rloeResults.getResults());
			mav.addObject("user",user);
			return mav;
    }
    
    //用户密码修改页面跳转
    @RequestMapping("/userPassword")
    public  ModelAndView userPasswordInfo(String userId) throws Exception{
		ModelAndView mav=new ModelAndView();	
    	if(userId==null){
    			SessionUserHelper helper = SessionUserHelper.createBySession(session);
       	  		LoginAccount la= helper.getLoginAccount();
       	  		Map<String, Object> user=userService.findUserById(la.getUserId());
       	  		mav.setViewName("view/user/userPassword");
       	  		mav.addObject("user",user);
    		return mav;
    			
    		}else {
    			Map<String, Object> user=userService.findUserById(userId);
        		mav.setViewName("view/user/userPassword");
        		mav.addObject("user",user);
        		return mav;
    		}
    		
    }
    
    
    
    //修改用户密码
    @RequestMapping("/updateUserPsd")
    public @ResponseBody String updateUserPsdInfo(@RequestParam("jyhmm") String jyhmm,@RequestParam("xyhmm") String xyhmm,@RequestParam("yhm") String yhm) throws Exception{
    	Map<String, Object> user=userService.findUserById(yhm);
		if(user.get("yhmm").equals(jyhmm.trim())){
			userService.modifyByUserpsd(xyhmm, yhm);
			JsonObject json  = new JsonObject() ; 
			json.addProperty("rs","1") ; 
			return json.toString();
		}else{			
			JsonObject json  = new JsonObject() ; 
			json.addProperty("rs","2") ; 
			return json.toString();
		}
    }
    
    //修改用户权限
    @RequestMapping("/updateUserRole")
    public @ResponseBody ModelAndView updateUserRoleInfo(@RequestParam("roleid") String roleid,@RequestParam("yhm") String yhm) throws Exception{
		userService.modifyByUserRole(yhm, roleid);
		ModelAndView mav=new ModelAndView("view/user/userRole");
		return mav;
    }
}
